package com.template.spring.beerportal.service;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.template.spring.beerportal.domain.Bar;
import com.template.spring.beerportal.domain.BarDetails;
import com.template.spring.beerportal.domain.Beer;
import com.template.spring.beerportal.domain.Customer;

public class Bootstrap implements InitializingBean {

	private transient final Log log = LogFactory.getLog(getClass());

	@Autowired
	private PlatformTransactionManager transactionManager;

	public Bootstrap() {

	}

	private boolean guestUserExists() {
		return Customer.findCustomer(1l) != null;
	}

	protected void insertBoostrapData() throws Exception {
		// if the guest user account exists, take this as a sign that the
		// bootstrap data populator has
		// already been run, so just exit quietly:
		if (guestUserExists()) {
			if (log.isDebugEnabled()) {
				log.debug("Bootstrap data insertion has already occurred. Returning quietly.");
			}
			return;
		}

		if (log.isInfoEnabled()) {
			log.info("Inserting bootstrap data...");
		}

		// otherwise, populate bootstrap data:
		Beer beer1 = new Beer();
		beer1.setName("Żubr");
		beer1.persist();
		
		Beer beer2 = new Beer();
		beer2.setName("Tatra");
		beer2.persist();
		
		Beer beer3 = new Beer();
		beer3.setName("Perła");
		beer3.persist();
		


		Customer customer1 = new Customer();
		customer1.setName("Rafcio");
		customer1.setMoney(30);
		customer1.persist();
		
		Customer customer2 = new Customer();
		customer2.setName("Erjon");
		customer2.setMoney(5);
		customer2.persist();
		
		Set<Customer> customers = new HashSet<Customer>();
		customers.add(customer1);
		customers.add(customer2);
		
		Bar bar1 = new Bar();
		bar1.setBarName("U Grubego");	
		
		Set<Beer> beers = new HashSet<Beer>();
		beers.add(beer1);
		beers.add(beer2);
		beers.add(beer3);
		bar1.setBeers(beers);
		bar1.persist();
		bar1.flush();
		
		BarDetails barDetails1 = new BarDetails();
		barDetails1.setAddress("Budryka 7/108A");
		barDetails1.setPhone("12345678");
		barDetails1.setBar(bar1);
		barDetails1.persist();
		
//		bar1.setCustomers(customers);
//		bar1.persist();
//		bar1.flush();
		
		customer1.setBar(bar1);
		customer1.persist();
		
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		log.info("Running data Bootstrap...");
		
		if (this.transactionManager == null) {
			throw new IllegalStateException(
					"transactionManager property must be set.");
		}
		// assert other injected dependencies here

		TransactionTemplate txnTemplate = new TransactionTemplate(this.transactionManager);
		txnTemplate.afterPropertiesSet();

		txnTemplate.execute(new TransactionCallback() {
			public Object doInTransaction(TransactionStatus status) {
				try {
					insertBoostrapData();
				} catch (Exception e) {
					// error - roll back. Runtime exception triggers rollback:
					if (e instanceof RuntimeException) {
						throw (RuntimeException) e;
					} else {
						throw new RuntimeException(e);
					}
				}
				return null;
			}
		});
	}

	
}
